package com.hsjk.szwj.module.system.controller.admin.service;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.hsjk.szwj.framework.common.pojo.PageResult;
import com.hsjk.szwj.framework.common.pojo.CommonResult;
import static com.hsjk.szwj.framework.common.pojo.CommonResult.success;

import com.hsjk.szwj.framework.excel.core.util.ExcelUtils;

import com.hsjk.szwj.framework.operatelog.core.annotations.OperateLog;
import static com.hsjk.szwj.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.hsjk.szwj.module.system.controller.admin.service.vo.*;
import com.hsjk.szwj.module.system.dal.dataobject.service.TenantServiceDO;
import com.hsjk.szwj.module.system.convert.service.TenantServiceConvert;
import com.hsjk.szwj.module.system.service.service.TenantServiceService;

@Tag(name = "管理后台 - 套餐服务关联")
@RestController
@RequestMapping("/system/tenant-service")
@Validated
public class TenantServiceController {

    @Resource
    private TenantServiceService tenantServiceService;

    @PostMapping("/create")
    @Operation(summary = "创建套餐服务关联")
    @PreAuthorize("@ss.hasPermission('system:tenant:create')")
    public CommonResult<Long> createTenantService(@Valid @RequestBody TenantServiceCreateReqVO createReqVO) {
        return success(tenantServiceService.createTenantService(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新套餐服务关联")
    @PreAuthorize("@ss.hasPermission('system:tenant:update')")
    public CommonResult<Boolean> updateTenantService(@Valid @RequestBody TenantServiceUpdateReqVO updateReqVO) {
        tenantServiceService.updateTenantService(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除套餐服务关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:tenant:delete')")
    public CommonResult<Boolean> deleteTenantService(@RequestParam("id") Long id) {
        tenantServiceService.deleteTenantService(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得套餐服务关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:tenant:query')")
    public CommonResult<TenantServiceRespVO> getTenantService(@RequestParam("id") Long id) {
        TenantServiceDO tenantService = tenantServiceService.getTenantService(id);
        return success(TenantServiceConvert.INSTANCE.convert(tenantService));
    }

    @GetMapping("/list")
    @Operation(summary = "获得套餐服务关联列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('system:tenant:query')")
    public CommonResult<List<TenantServiceRespVO>> getTenantServiceList(@RequestParam("ids") Collection<Long> ids) {
        List<TenantServiceDO> list = tenantServiceService.getTenantServiceList(ids);
        return success(TenantServiceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得套餐服务关联分页")
    @PreAuthorize("@ss.hasPermission('system:tenant:query')")
    public CommonResult<PageResult<TenantServiceRespVO>> getTenantServicePage(@Valid TenantServicePageReqVO pageVO) {
        PageResult<TenantServiceDO> pageResult = tenantServiceService.getTenantServicePage(pageVO);
        return success(TenantServiceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出套餐服务关联 Excel")
    @PreAuthorize("@ss.hasPermission('system:tenant:export')")
    @OperateLog(type = EXPORT)
    public void exportTenantServiceExcel(@Valid TenantServiceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<TenantServiceDO> list = tenantServiceService.getTenantServiceList(exportReqVO);
        // 导出 Excel
        List<TenantServiceExcelVO> datas = TenantServiceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "套餐服务关联.xls", "数据", TenantServiceExcelVO.class, datas);
    }

}
